package com.ruoyi.project.system.area.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.framework.web.domain.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.area.mapper.AreaMapper;
import com.ruoyi.project.system.area.domain.Area;
import com.ruoyi.project.system.area.service.IAreaService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 区域Service业务层处理
 * 
 * @author Leil
 * @date 2020-04-21
 */
@Service
public class AreaServiceImpl implements IAreaService 
{
    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询区域
     * 
     * @param id 区域ID
     * @return 区域
     */
    @Override
    public Area selectAreaById(Integer id)
    {
        return areaMapper.selectAreaById(id);
    }

    /**
     * 查询区域列表
     * 
     * @param area 区域
     * @return 区域
     */
    @Override
    public List<Area> selectAreaList(Area area)
    {
        return areaMapper.selectAreaList(area);
    }

    /**
     * 新增区域
     * 
     * @param area 区域
     * @return 结果
     */
    @Override
    public int insertArea(Area area)
    {
        return areaMapper.insertArea(area);
    }

    /**
     * 修改区域
     * 
     * @param area 区域
     * @return 结果
     */
    @Override
    public int updateArea(Area area)
    {
        return areaMapper.updateArea(area);
    }

    /**
     * 删除区域对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAreaByIds(String ids)
    {
        return areaMapper.deleteAreaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除区域信息
     * 
     * @param id 区域ID
     * @return 结果
     */
    @Override
    public int deleteAreaById(Integer id)
    {
        return areaMapper.deleteAreaById(id);
    }

    @Override
    public List<Ztree> getAreaTreeData() {
        List<Area> areas = this.areaMapper.selectAreaList(null);
        return this.getAreaTree(areas);
    }

    /**
     * 获取区域树形列表
     */
    private List<Ztree> getAreaTree(List<Area> areas) {
        List<Ztree> ztrees = areas.stream()
                .map(area -> {
                    Ztree ztree = new Ztree();
                    ztree.setId((long) area.getId());
                    ztree.setpId((long) area.getPid());
                    ztree.setName(area.getAreaName());
                    ztree.setTitle(area.getAreaName());
                    return ztree;
                }).collect(Collectors.toList());
        return ztrees;
    }
}
